package org.example.network.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server01 {

    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(8888);

        while(true)
        {
            Socket socket = serverSocket.accept();

            System.out.println("有客户端连接到我服务器了!");

            ServerThread serverThread = new ServerThread(socket);
            serverThread.start();

        }


    }

}


class ServerThread extends Thread{

    private Socket socket;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

        PrintStream printStream = null;
        try {
            printStream = new PrintStream(socket.getOutputStream());

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String line = null;
            while( (line = bufferedReader.readLine()) != null ){

                System.out.println(line);

                printStream.println("return: " + line);
                printStream.flush();
            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}